<template>
  <div class="createnews">
    <h2>{{id?'编辑':'创建'}}信息</h2>
    <el-form ref="form" :model="form" label-width="80px" size="medium ">
      <el-form-item label="标题" prop="article_title"
                    :rules="{required:true,message:'标题不能为空',trigger:'blur'}">
        <el-input v-model="form.article_title"></el-input>
      </el-form-item>
      <el-form-item label="简介">
        <el-input type="textarea" :rows="2" placeholder="请输入文章简介" v-model="form.article_introduction"></el-input>
      </el-form-item>
      <el-form-item label="分类">
        <el-radio
          v-for="(item,id) in lable"
          :key="id"
          v-model="form.article_lable"
          :label="item"
        >{{item}}</el-radio>
      </el-form-item>
      <el-form-item label="内容">
        <vue-editor
          useCustomImageHandler
          @image-added="handleImageAdded"
          v-model="form.article_content"
        ></vue-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="$router.push('/admin/newslist')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'createnews',
  data () {
    return {
      lable: [],
      dialogVisible: false,
      form: {}
    }
  },
  props: {
    id: {}
  },
  methods: {
    // 富文本编辑器图片上传
    async handleImageAdded (file, Editor, cursorLocation, resetUploader) {
      const formData = new FormData()
      formData.append('file', file)
      const res = await this.$axios.post('/uploads', formData)
      console.log(res)
      Editor.insertEmbed(cursorLocation, 'image', res.data.url)
      resetUploader()
    },
    // 发布与编辑
    async onSubmit () {
      if (!this.form.article_title) {
        this.$message.error('请先输入标题')
        return
      }
      let result
      console.log(this.form)
      if (this.id) {
        this.form.id = this.id
        result = await this.$axios.post('/webadmin/updatearticle', this.qs.stringify(this.form))
      } else {
        result = await this.$axios.post('/webadmin/createarticle', this.qs.stringify(this.form))
      }
      if (result.data.state.type === 'SUCCESS') {
        this.$message.success('成功')
        this.$router.push('/admin/newslist')
      }
    },
    async getArticleDetails () {
      const result = await this.$axios.post('/webadmin/getarticledetails',
        this.qs.stringify({ id: this.id }))
      this.form = result.data.data
    },
    async lablelist () {
      const result = await this.$axios.post('/admin/lablelist',
        this.qs.stringify({ lable_name: '文章分类' }))
      if (result.data.state.type === 'SUCCESS') {
        this.lable = JSON.parse(result.data.data[0].lable)
      }
    }
  },
  created () {
    this.id && this.getArticleDetails()
    this.lablelist()
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
